package editor.UI
{
	import fl.controls.ScrollBarDirection;
	import fl.controls.TileList;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import game.manager.ConfigurationManager;
	import game.manager.TemplateTileManager;
	import game.VO.TileTemplateVO;
	import starling.core.Starling;
	import starling.events.ResizeEvent;
	
	/**
	 * @author Kirill Pletnev
	 */
	public class RightEditorPanel extends Sprite
	{
		private static const LEFT_PANEL_WIDTH:int = 80;
		
		private var _tileList:TileList;
		private var _selectedTileTemplateVO:TileTemplateVO;
		
		public function RightEditorPanel()
		{
		}
		
		public function init():void
		{
			_tileList = new TileList();			
			_tileList.width = LEFT_PANEL_WIDTH;
			_tileList.columnWidth = LEFT_PANEL_WIDTH - 15;
			_tileList.direction = ScrollBarDirection.VERTICAL;
			_tileList.addEventListener(Event.CHANGE, onSelectTile);
			addChild(_tileList);
			
			updateTilesContent();
			
			Starling.current.stage.addEventListener(ResizeEvent.RESIZE, resizeStage);
			
			resizeStage();
		}
		
		public function updateTilesContent():void
		{
			_tileList.removeAll();
			
			for each (var tileVO:TileTemplateVO in TemplateTileManager.instance.tiles)
			{
				var tilePrew:Object = new Object();
				tilePrew.label = "";
				tilePrew.id = tileVO.id;
				tilePrew.source = ConfigurationManager.instance.pathToTileRes + tileVO.textureURL;
				
				_tileList.addItem(tilePrew);
			}
		}
		
		private function onSelectTile(event:Event):void
		{
			var selectedTile:Object = _tileList.dataProvider.getItemAt(_tileList.selectedIndex);
			var tileId:int = selectedTile.id;
			
			_selectedTileTemplateVO = TemplateTileManager.instance.getTemplateTileById(tileId);
		}
		
		private function resizeStage(event:ResizeEvent = null):void
		{
			var viewPort:Rectangle = Starling.current.viewPort;
			
			_tileList.x = viewPort.width - LEFT_PANEL_WIDTH;			
			_tileList.height = viewPort.height - this.y;
		}
		
		public function get selectedTileTemplateVO():TileTemplateVO 
		{
			return _selectedTileTemplateVO;
		}
	}
}